package com.sjgs.gis.spark;


import org.apache.spark.SparkContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;


@ConditionalOnProperty(value = "geospark.enabled", havingValue = "true")
@RestController
@RequestMapping("/api")
public class TileServiceController {

    private final SparkContext sparkContext;
    private final SparkTileMapService sparkTileMapService;

    public TileServiceController(SparkContext sparkContext, SparkTileMapService sparkTileMapService) {
        this.sparkContext = sparkContext;
        this.sparkTileMapService = sparkTileMapService;
    }

    /**
     * 读取geotrellis瓦片
     * @param layer
     * @param z
     * @param x
     * @param y
     * @param response
     */
    @GetMapping("/tile/{layer}/{z}/{x}/{y}.png")
    public void getTile(@PathVariable("layer") String layer,
                        @PathVariable("z") Integer z,
                        @PathVariable("x") Integer x,
                        @PathVariable("y") Integer y,
                        HttpServletResponse response
    ) {
        sparkTileMapService.readTileLayer(sparkContext, "fs", response, layer, z, x, y);
    }

    /**
     * 分布式写geotrellis瓦片
     * @param layer
     * @param inputPath
     * @param outputPath
     */
    @PostMapping("/tile/{layer}")
    public void writeTile(@PathVariable("layer") String layer,
                          @RequestParam("input") String inputPath,
                          @RequestParam("output") String outputPath
    ) {
        sparkTileMapService.writeTileLayer(sparkContext, layer, inputPath, outputPath);
    }
}
